import Request from "@/sdk/pocky-request/index.js"; //引入
import amap from "../../common/amap-wx.js";

/**
 * map store 地图/定位模块
 */
const map = {
	// 状态
	state: {
		selectCity: {
			cityName: '南京市',
			cityCode: 3201
		},
		locationCity: {
			"city": "南京市",
			"province": "江苏省",
			"adcode": "320104",
			"district": "秦淮区",
			"towncode": "320104006000",
			"country": "中国",
			"township": "五老村街道",
			"citycode": "025"
		},
		roads:{
			longitude:118.794103,
			latitude:32.040116
		},
	},
	// 内部派生
	getters: {

	},
	// 同步方法 commit
	mutations: {
		selectCity: (state, payload) => {
			state.selectCity = payload
		},
		locationCity: (state, payload) => {
			state.locationCity = payload
		},
		roads: (state, payload) => {
			state.roads = payload
		},
	},
	// 异步方法 dispatch
	actions: {
		// 获取当前定位城市
		getLocationCity(context) {
			return new Promise((resolve,reject)=>{
				
				let amapPlugin = new amap.AMapWX({
					key: '0d572b9da42e82fb0021e3f5a12a8fd7',
				});
				amapPlugin.getRegeo({
					success: (data) => {
						let _areaId=parseInt(data[0].regeocodeData.addressComponent.adcode.toString().substr(0,4));
						let _cityArr=context.rootState.sys.initInfo.open_city;
						if(_cityArr.indexOf(_areaId)>-1){
							this.commit("locationCity", data[0].regeocodeData.addressComponent)
							this.commit("roads",{
								longitude:data[0].longitude,
								latitude:data[0].latitude
							});
							this.commit("selectCity",{
								cityName: data[0].regeocodeData.addressComponent.city,
								cityCode: _areaId
							})
							resolve(data[0])
						}else{
							uni.showToast({
								title: '您所在的城市暂未开通，敬请期待',
								icon: 'none'
							});
							this.commit("selectCity",{
								cityName:'南京市',
								cityCode: 3201
							})
							this.commit("locationCity", {
								"city": "南京市",
								"province": "江苏省",
								"adcode": "320104",
								"district": "秦淮区",
								"towncode": "320104006000",
								"country": "中国",
								"township": "五老村街道",
								"citycode": "025"
							})
							this.commit("roads", {
								longitude: data[0].longitude,
								latitude: data[0].latitude
							})
							reject(info)
						}
						
					},
					fail: (info)=>{
						//失败回调
						
						this.commit("locationCity", {
							"city": "南京市",
							"province": "江苏省",
							"adcode": "320104",
							"district": "秦淮区",
							"towncode": "320104006000",
							"country": "中国",
							"township": "五老村街道",
							"citycode": "025"
						})
						this.commit("selectCity",{
							cityName:'南京市',
							cityCode: 3201
						})
						this.commit("roads", {
							longitude:118.794103,
							latitude:32.040116
						})
						reject(info)
					}
				
				});
				
			})
			
			
		},
		/**
		 * @description 变化定位时处理
		 * @param {Object} context
		 */
		changeRoads(context) {
		
			let amapPlugin = new amap.AMapWX({
				key: '0d572b9da42e82fb0021e3f5a12a8fd7',
			});
			amapPlugin.getRegeo({
				success: (data) => {
					if (context.state.longitude != data[0].longitude) { //定位发生变化
						this.commit("roads", {
							longitude: data[0].longitude,
							latitude: data[0].latitude
						})
						uni.$emit("roadsChange")
					}
				},
				fail: (info) => {
					//失败回调
		
					this.commit("locationCity", {
						"city": "南京市",
						"province": "江苏省",
						"adcode": "320104",
						"district": "秦淮区",
						"towncode": "320104006000",
						"country": "中国",
						"township": "五老村街道",
						"citycode": "025"
					})
					this.commit("selectCity", {
						cityName: '南京市',
						cityCode: 3201
					})
					this.commit("roads", {
						longitude: 118.794103,
						latitude: 32.040116
					})
		
		
				}
		
			});
		
		},
		choseCity() {

		},
		/**
		 * @description 获取获取商圈
		 * @param {Object} context
		 * @param {Object} _areaId
		 */
		apiGetBusCircle(context,_areaId){
			return new Promise((resolve,reject) => {
				Request().request({
					url: '/mall/street/list.do',
					method: 'post',
					data: {
						areaId:_areaId
					}
				}).then(res => {
					if (res.data.result == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '商圈获取失败,' + res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		}
	}
}

export default map
